<?php

namespace App\Filament\Admin\Resources\ComponentTypeResource\Pages;

use App\Filament\Admin\Resources\ComponentTypeResource;
use App\Models\ComponentType;
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;
use App\Models\Component as ComponentModel;

class CreateComponentType extends CreateRecord
{
    protected static string $resource = ComponentTypeResource::class;

    protected function mutateFormDataBeforeCreate(array $data): array
    {
        $component_tree = $data['component_tree'];
        $demo_data = $data['demo_data'];
        $component_id = $data['component_id'];

        if(!empty($demo_data)){
            ComponentModel::findOrFail($component_id)->update([
                'demo_data' => $demo_data,
            ]);
        }

        if(!empty($component_tree)){
            ComponentModel::findOrFail($component_id)->update([
                'component_tree' => $component_tree,
            ]);
        }

        unset($data['demo_data']);
        unset($data['component_tree']);

        return $data;
    }
}
